package com.bluegalaxy.nhb.snservice.service.execute.impl;

import com.bluegalaxy.nhb.snservice.common.utils.StringUtil;
import com.bluegalaxy.nhb.snservice.domain.constant.CkConstant;
import com.bluegalaxy.nhb.snservice.domain.enums.BaseUserEnum;
import com.bluegalaxy.nhb.snservice.domain.props.CkDbBaseProperties;
import com.bluegalaxy.nhb.snservice.service.execute.ExecuteService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;
import org.springframework.util.FileCopyUtils;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.Map;

/**
 * @author:
 * @date: 2021/6/23 12:59
 * @description:
 */
@Service("ExecuteService")
public class ExecuteServiceImpl implements ExecuteService {

    private static Log log = LogFactory.getLog(ExecuteServiceImpl.class);

    @Autowired
    private CkDbBaseProperties dbBaseProperties;

    @Override
    public boolean createTable(String tableName,String tableType){
        Connection conn = null;
        Statement stat = null;

        String url = dbBaseProperties.getUrl();
        String username = dbBaseProperties.getUsername();
        String password = dbBaseProperties.getPassword();

        try {
            //连接数据库
            Class.forName(dbBaseProperties.getDriver());
            //测试url中是否包含useSSL字段，没有则添加设该字段且禁用
            conn = DriverManager.getConnection(url, username, password);
            stat = conn.createStatement();
            String createSql = CkConstant.SQL_CREATE_GROUP;
            if (BaseUserEnum.TAGS.name().equals(tableType)){
                createSql = CkConstant.SQL_CREATE_TAGS;
            }else if(BaseUserEnum.PORTRAIT.name().equals(tableType)){
                createSql = CkConstant.SQL_CREATE_PORTRAIT;
            }
            ClassPathResource classPathResource = new ClassPathResource("sql/"+createSql);

            InputStream inputStream =classPathResource.getInputStream();
            byte[] bdata = FileCopyUtils.copyToByteArray(inputStream);
            createSql = new String(bdata, StandardCharsets.UTF_8);

            createSql = createSql.replaceAll("table_name",tableName);

            if (StringUtil.isEmpty(createSql)){
                log.info("建表语句为空。。。。。。。。。。");
                return false;
            }
            stat.executeUpdate(createSql);
//            String[] sqlstrs = createSql.split("GO");
//            for (String sql : sqlstrs){
//                if (StringUtil.isNotEmpty(sql)){
//                    stat.executeUpdate(sql);
//                }
//            }
        } catch (IOException | SQLException | ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            // 释放资源
            try {
                if (stat != null){
                    stat.close();
                }
                if (conn != null){
                    conn.close();
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }

        }

        return true;
    }

    @Override
    public boolean insertUsers(List<Map<String, Object>> userList) {
        return false;
    }
}
